package com.example.xposed

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.telephony.TelephonyManager
import android.util.Log
import android.widget.Button
import android.widget.RadioButton
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    val tv_location: TextView by lazy {
        findViewById(R.id.tv_location)
    }
    val edit_latitude: TextView by lazy {
        findViewById(R.id.edit_GPSLatitude)
    }
    val edit_longitude: TextView by lazy {
        findViewById(R.id.edit_GPSLongitude)
    }
    val btn_save: TextView by lazy {
        findViewById(R.id.btn_save)
    }
    val radio_earth: RadioButton by lazy {
        findViewById(R.id.radio_earth)
    }
    val radio_mars: RadioButton by lazy {
        findViewById(R.id.radio_Mars)
    }
    val radio_baidu: RadioButton by lazy {
        findViewById(R.id.radio_bd09)
    }

    val btn_getLocation: Button by lazy {
        findViewById(R.id.btn_getLocation)
    }

    val TAG = "MainActivity"

    @SuppressLint("MissingPermission", "CommitPrefEdits")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var sharedPreferences = getSharedPreferences("location", MODE_PRIVATE)
        var string = sharedPreferences.getString("GPSLatitude", "0")
        var string1 = sharedPreferences.getString("GPSLongitude", "0")
        edit_latitude.text = string
        edit_longitude.text = string1
        tv_location.text = "经度:$string1 纬度:$string \n 注意:有些app会使用蓝牙定位  记得关闭蓝牙"
        btn_save.setOnClickListener {
            var longitudeString = edit_longitude.text.toString()
            var latitudeString = edit_latitude.text.toString()
            if (longitudeString.isEmpty() || latitudeString.isEmpty()) {
                Toast.makeText(this@MainActivity, "请输入", Toast.LENGTH_LONG).show()
                return@setOnClickListener
            }
            val context: Context = this@MainActivity
            val sp = context.getSharedPreferences("location", MODE_PRIVATE).edit()
            var latitude = latitudeString.toDouble()
            var longitude = longitudeString.toDouble()
            var gps = Gps(latitude, longitude)
            if (radio_mars.isChecked) {
                gps = PositionUtil.gcj_To_Gps84(latitude, longitude)
                Log.d(TAG, " 火星坐标系")
            } else if (radio_earth.isChecked) {
                Log.d(TAG, "地球坐标系")
            } else if (radio_baidu.isChecked) {
                gps = PositionUtil.bd09_To_Gps84(latitude, longitude)
                Log.d(TAG, "百度坐标系")
            }

            latitude = gps.wgLat
            longitude = gps.wgLon
            sp.putString("GPSLatitude", latitude.toString())
            sp.putString("GPSLongitude", longitude.toString())
            sp.commit()
            Toast.makeText(this, "修改成功", Toast.LENGTH_LONG).show()
            sendLocation()
        }
        btn_getLocation.setOnClickListener {
            var s = "https://api.map.baidu.com/lbsapi/getpoint/index.html?qq-pf-to=pcqq.group"
            var parse = Uri.parse(s)
            var intent = Intent()
            intent.setAction("android.intent.action.VIEW")
            intent.setData(parse)
            startActivity(intent)
        }
        sendLocation()
//        test()
    }

    override fun onResume() {
        super.onResume()

    }
    private fun sendLocation() {
        val sp = getSharedPreferences("location", MODE_PRIVATE)
        val intent = Intent(UseGps.action)
        intent.putExtra("GPSLatitude", java.lang.Double.valueOf(sp.getString("GPSLatitude", "0")))
        intent.putExtra("GPSLongitude", java.lang.Double.valueOf(sp.getString("GPSLongitude", "0")))
        sendBroadcast(intent)
    }


    private fun test() {
        val context = this.applicationContext
       var managet: TelephonyManager

        val call = context.contentResolver.call(
            Uri.parse("content://" + BuildConfig.APPLICATION_ID + ".LocationManager"),
            "",
            "",
            null
        )
        if (call != null) {
            UseGps.latitude = call.getDouble("GPSLatitude", 0.0)
            UseGps.longitude = call.getDouble("GPSLongitude", 0.0)
        }
        Log.d(
            UseGps.TAG,
            "locationInit: latitude=" + UseGps.latitude + "  longitude=" + UseGps.longitude
        )

    }
}